/*
 * @Descripttion:
 * @version:
 * @Author: sueRimn
 * @Date: 2025-06-02 23:01:30
 * @LastEditors: sueRimn
 * @LastEditTime: 2025-10-08 16:08:40
 */

#include "commun.h"
#include "string.h"
#include "stdio.h"
#include "public.h"
#include "step.h"
#include "led.h"
#include "power.h"

#include "can.h"

void framCheck(uint8_t *pucFram, uint8_t ucFramLen)
{
    uint8_t ucSum = 0;
    for (uint8_t i = 0;i < (ucFramLen - 1);i++)
        ucSum += *(pucFram + i);

    if (!(ucSum == *(pucFram + ucFramLen - 1)))
    {
        printf("ucSum = %x  framSum = %x\r\n", ucSum, *(pucFram + ucFramLen - 1));
        return;		//判断sum
    }

    if (!(*(pucFram) == 0xAA && *(pucFram + 1) == 0xAF))
    {
        printf("head1 = %x  head2 = %x\r\n", *(pucFram), *(pucFram + 1));
        return;		//判断帧头
    }


    pstDev->bIsGetGTMessage = true;
    memcpy(pstDev->pstRecvMessage, pucFram, ucFramLen);
    printf("cmd = %x data len = %d\r\n", pstDev->pstRecvMessage->ucCmd, pstDev->pstRecvMessage->ucDataLen);
}

void messageOperation()
{
    if (pstDev->bIsGetGTMessage == true)
    {
        switch (pstDev->pstRecvMessage->ucCmd)
        {
            case 0x01:

                break;

            default:
                break;
        }
    }
    pstDev->bIsGetGTMessage = false;
}


/*******************************************can*********************************************************/

void canReportPower(void)
{
    uint32_t uiCout = 0;

    //		if(!pstDev->bIsRecvCanMessageFlag)
    //			return;

    pstDev->stCanTxHeader.StdId = CMD_REPORT_POWER;
    pstDev->stCanTxHeader.DLC = 8;

    pstDev->aucCanTxBuf[0] = (uint8_t)(pstDev->stDevBat.sVolVal >> 8);
    pstDev->aucCanTxBuf[1] = (uint8_t)(pstDev->stDevBat.sVolVal);
    pstDev->aucCanTxBuf[2] = (uint8_t)(pstDev->stDevBat.sEleVal >> 8);
    pstDev->aucCanTxBuf[3] = (uint8_t)(pstDev->stDevBat.sEleVal);
    pstDev->aucCanTxBuf[4] = (uint8_t)(pstDev->stDevCharg.sVolVal >> 8);
    pstDev->aucCanTxBuf[5] = (uint8_t)(pstDev->stDevCharg.sVolVal);
    pstDev->aucCanTxBuf[6] = (uint8_t)(pstDev->stDevCharg.sEleVal >> 8);
    pstDev->aucCanTxBuf[7] = (uint8_t)(pstDev->stDevCharg.sEleVal);

    while (HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) == 0)
    {
        uiCout++;
        if (uiCout > 5000)
            break;
    }

    HAL_CAN_AddTxMessage(&hcan1, &pstDev->stCanTxHeader, pstDev->aucCanTxBuf, &pstDev->uiCanTxMailbox);
}

void canReportDistance(void)
{

    //	if(!pstDev->bIsRecvCanMessageFlag)
    //			return;

    pstDev->stCanTxHeader.StdId = CMD_REPORT_DISTANC;
    pstDev->stCanTxHeader.DLC = 8;

    pstDev->aucCanTxBuf[0] = 0;

    if (pstDev->stDevDyp1.iDistance == 0)
        pstDev->aucCanTxBuf[0] |= 0x01;
    if (pstDev->stDevDyp2.iDistance == 0)
        pstDev->aucCanTxBuf[0] |= 0x02;
    if (pstDev->stDevDyp3.iDistance == 0)
        pstDev->aucCanTxBuf[0] |= 0x04;

    pstDev->aucCanTxBuf[1] = (uint8_t)(pstDev->stDevDyp1.iDistance >> 8);
    pstDev->aucCanTxBuf[2] = (uint8_t)(pstDev->stDevDyp1.iDistance);
    pstDev->aucCanTxBuf[3] = (uint8_t)(pstDev->stDevDyp2.iDistance >> 8);
    pstDev->aucCanTxBuf[4] = (uint8_t)(pstDev->stDevDyp2.iDistance);
    pstDev->aucCanTxBuf[5] = (uint8_t)(pstDev->stDevDyp3.iDistance >> 8);
    pstDev->aucCanTxBuf[6] = (uint8_t)(pstDev->stDevDyp3.iDistance);

    pstDev->aucCanTxBuf[7] = 0;

    if (pstDev->stDevFall.bIsFall1 == true)
        pstDev->aucCanTxBuf[7] |= 0x01;
    if (pstDev->stDevFall.bIsFall2 == true)
        pstDev->aucCanTxBuf[7] |= 0x02;
    if (pstDev->stDevFall.bIsFall3 == true)
        pstDev->aucCanTxBuf[7] |= 0x04;
    if (pstDev->stDevFall.bIsFall4 == true)
        pstDev->aucCanTxBuf[7] |= 0x08;

    HAL_CAN_AddTxMessage(&hcan1, &pstDev->stCanTxHeader, pstDev->aucCanTxBuf, &pstDev->uiCanTxMailbox);
}

void canReportIcmAcc(void)
{
    //	if(!pstDev->bIsRecvCanMessageFlag)
    //			return;

    pstDev->stCanTxHeader.StdId = CMD_REPORT_ICM_ACC;
    pstDev->stCanTxHeader.DLC = 6;

    if (pstDev->stDevIcm.eDevStat == SEN_ERR)
    {
        pstDev->aucCanTxBuf[1] = 0xFF;
        pstDev->aucCanTxBuf[2] = 0xFF;
        pstDev->aucCanTxBuf[3] = 0xFF;
        pstDev->aucCanTxBuf[4] = 0xFF;
        pstDev->aucCanTxBuf[5] = 0xFF;
        pstDev->aucCanTxBuf[6] = 0xFF;
    }
    else
    {
        pstDev->aucCanTxBuf[0] = (uint8_t)(pstDev->stDevIcm.aucAccArr[0] >> 8);
        pstDev->aucCanTxBuf[1] = (uint8_t)(pstDev->stDevIcm.aucAccArr[0]);
        pstDev->aucCanTxBuf[2] = (uint8_t)(pstDev->stDevIcm.aucAccArr[1] >> 8);
        pstDev->aucCanTxBuf[3] = (uint8_t)(pstDev->stDevIcm.aucAccArr[1]);
        pstDev->aucCanTxBuf[4] = (uint8_t)(pstDev->stDevIcm.aucAccArr[2] >> 8);
        pstDev->aucCanTxBuf[5] = (uint8_t)(pstDev->stDevIcm.aucAccArr[2]);
    }
    HAL_CAN_AddTxMessage(&hcan1, &pstDev->stCanTxHeader, pstDev->aucCanTxBuf, &pstDev->uiCanTxMailbox);
}
void canReportIcmGyro(void)
{

    //	if(!pstDev->bIsRecvCanMessageFlag)
    //			return;
    pstDev->stCanTxHeader.StdId = CMD_REPORT_ICM_GYRO;
    pstDev->stCanTxHeader.DLC = 6;

    if (pstDev->stDevIcm.eDevStat == SEN_ERR)
    {
        pstDev->aucCanTxBuf[1] = 0xFF;
        pstDev->aucCanTxBuf[2] = 0xFF;
        pstDev->aucCanTxBuf[3] = 0xFF;
        pstDev->aucCanTxBuf[4] = 0xFF;
        pstDev->aucCanTxBuf[5] = 0xFF;
        pstDev->aucCanTxBuf[6] = 0xFF;
    }
    else
    {
        pstDev->aucCanTxBuf[0] = (uint8_t)(pstDev->stDevIcm.aucGroArr[0] >> 8);
        pstDev->aucCanTxBuf[1] = (uint8_t)(pstDev->stDevIcm.aucGroArr[0]);
        pstDev->aucCanTxBuf[2] = (uint8_t)(pstDev->stDevIcm.aucGroArr[1] >> 8);
        pstDev->aucCanTxBuf[3] = (uint8_t)(pstDev->stDevIcm.aucGroArr[1]);
        pstDev->aucCanTxBuf[4] = (uint8_t)(pstDev->stDevIcm.aucGroArr[2] >> 8);
        pstDev->aucCanTxBuf[5] = (uint8_t)(pstDev->stDevIcm.aucGroArr[2]);
    }

    HAL_CAN_AddTxMessage(&hcan1, &pstDev->stCanTxHeader, pstDev->aucCanTxBuf, &pstDev->uiCanTxMailbox);
}

void canReportSwitch(uint8_t ucCmd, uint8_t ucSwitchStat, uint8_t ucFastStopFlag)
{

    //	if(!pstDev->bIsRecvCanMessageFlag)
    //			return;
    uint32_t uiCout = 0;

    pstDev->stCanTxHeader.StdId = CMD_REPORT_SWITCH;

    pstDev->aucCanTxBuf[0] = ucCmd;
    pstDev->aucCanTxBuf[1] = ucSwitchStat;
    pstDev->aucCanTxBuf[3] = ucFastStopFlag;

    while (HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) == 0)   //由于发送频率低 所以必须要确保能发送
    {
        uiCout++;
        if (uiCout > 5000)
            break;
    }
    HAL_CAN_AddTxMessage(&hcan1, &pstDev->stCanTxHeader, pstDev->aucCanTxBuf, &pstDev->uiCanTxMailbox);
}

void canTxMessagePackInit(void)
{


    uint32_t TxMailbox;

    // TxHeader.StdId = uiStID;
    pstDev->stCanTxHeader.ExtId = 0;
    pstDev->stCanTxHeader.RTR = CAN_RTR_DATA;
    pstDev->stCanTxHeader.IDE = CAN_ID_STD;
    pstDev->stCanTxHeader.DLC = 8;
    pstDev->stCanTxHeader.TransmitGlobalTime = DISABLE;
}

/********************************************can motor******************************************************************/
void canCtrlMotor(uint8_t ucMotorAddr, uint8_t ucCmd, uint16_t usObject1, uint8_t ucObjcet2, uint8_t *pucData)
{


    uint32_t uiCout = 0;

    pstDev->stCanTxHeader.StdId = 0x600 | ucMotorAddr;

    pstDev->aucCanTxBuf[0] = ucCmd;
    pstDev->aucCanTxBuf[1] = (usObject1 >> 8) & 0xFF;
    pstDev->aucCanTxBuf[2] = usObject1 & 0xFF;
    pstDev->aucCanTxBuf[3] = ucObjcet2 & 0xFF;

    pstDev->aucCanTxBuf[4] = pucData[0];
    pstDev->aucCanTxBuf[5] = pucData[1];
    pstDev->aucCanTxBuf[6] = pucData[2];
    pstDev->aucCanTxBuf[7] = pucData[3];

    while (HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) == 0)   //由于发送频率低 所以必须要确保能发送
    {
        uiCout++;
        if (uiCout > 5000)
            break;
    }
    HAL_CAN_AddTxMessage(&hcan1, &pstDev->stCanTxHeader, pstDev->aucCanTxBuf, &pstDev->uiCanTxMailbox);
}

void canForward(uint8_t ucMotorAddr, uint8_t *pucData)
{
    uint32_t uiCout = 0;

    pstDev->stCanTxHeader.StdId = 0x600 | ucMotorAddr;

    memcpy(pstDev->aucCanTxBuf, pucData, 8);



    while (HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) == 0)   //由于发送频率低 所以必须要确保能发送
    {
        uiCout++;
        if (uiCout > 5000)
            break;
    }
    HAL_CAN_AddTxMessage(&hcan1, &pstDev->stCanTxHeader, pstDev->aucCanTxBuf, &pstDev->uiCanTxMailbox);
}

void canFramOperationCallback(CAN_RxHeaderTypeDef *pstCanRxHeader, uint8_t *pucCanRxBuf)
{
    static uint8_t cout = 0;
    if (pstCanRxHeader->IDE == CAN_ID_STD)
    {
        switch (pstCanRxHeader->StdId)
        {
            case CMD_RECV_LED:
                //                printf("can = %x %x %x %x\r\n",pucCanRxBuf[0],pucCanRxBuf[1],pucCanRxBuf[2],pucCanRxBuf[3]);
                setLedStat(pucCanRxBuf[0], pucCanRxBuf[1], pucCanRxBuf[2]);
                pstDev->bIsRecvCanMessageFlag = true;
                break;
            case CMD_RECV_POWER:
                powerCtrl(pucCanRxBuf);
                pstDev->bIsRecvCanMessageFlag = true;
                break;

            case CMD_RECV_MOTOR:
                printf("CMD_RECV_MOTOR\r\n");
                pstDev->bIsRecvCanMessageFlag = true;
                if (pucCanRxBuf[0] == 1)
                {

                }
                else
                {

                }
                break;
            case CMD_RECV_MAGENT:  // 
                magnetCtrl(pucCanRxBuf[0]);
                pstDev->bIsRecvCanMessageFlag = true;
                break;

            case CMD_REPORT_POWER:
                //                printf("CMD_REPORT_POWER\r\n");
                break;
            case CMD_REPORT_DISTANC:
                if (cout < 2)
                {
                    cout++;
                    printf("CMD_REPORT_DISTANC\r\n");
                }

                break;
            case CMD_REPORT_ICM_ACC:
                // printf("CMD_REPORT_ICM_ACC\r\n");
                break;
            case CMD_REPORT_ICM_GYRO:
                // printf("CMD_REPORT_ICM_GYRO\r\n");
                break;
                // case CMD_REPORT_SWITCH:
                //     printf("CMD_REPORT_SWITCH\r\n");
                //     break;

            case 0x400:
                printf("recv light %x %x\r\n",pucCanRxBuf[0],pucCanRxBuf[1]);
                break;
//						
//            default:
//                break;
        }
    }
    else
    {
        if ((pstCanRxHeader->ExtId & 0x601) == 0x601)
        {
            if (!pstDev->stDevMotor.bIsStop)
                canForward(0x01, pucCanRxBuf);
        }
        else if ((pstCanRxHeader->ExtId & 0x602) == 0x602)
        {
            if (!pstDev->stDevMotor.bIsStop)
                canForward(0x02, pucCanRxBuf);
        }
        // switch (pstCanRxHeader->ExtId)
        // {

        //     default:
        //         break;
        // }
    }
}

